<!DOCTYPE html>
<html xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>

<body>
<div class="layuimini-container" id="vue">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">任务名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="taskName" autocomplete="off" class="layui-input" id="taskName">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>

            </div>

        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>



    </div>
</div>
<script type="text/html" id="status">
    {{#  if(d.timeState === true ){ }}
    <button class="layui-btn layui-btn-xs layui-btn-normal">进行中</button>
    {{#  } }}
    {{#  if(d.timeState === false ){ }}
    <button class="layui-btn layui-btn-xs layui-btn-danger">已结束</button>
    {{#  } }}
</script>

<script src="../../lib/layui-v2.7.3/layui/layui.js" charset="utf-8"></script>
<script src="../../api/urlConfig.js"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="../../js/base64.min.js"></script>

<script>
    // 加载团队下拉菜单 teamList
    $(function () {
        $.ajax({
            url: ipURL + '/caption/team/list',
            type: 'GET',
            dataType: 'json',
            beforeSend: function (request) {
                request.setRequestHeader("token", localStorage.token);
            },
            success: function (data) {
                $.each(data.data.data, function (index, value) {
                    $('.teamList').append(new Option(value.name,value.id));// 下拉菜单里添加元素
                });
                layui.form.render("select");//重新渲染 固定写法

            },
        });
    })





    layui.use(['table', 'util'], function(){
        var table = layui.table
            ,util = layui.util;

        //监听单元格编辑
        table.on('edit(currentTableId)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
        });
    });

    layui.use(['form', 'table','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        table.render({
            elem: '#currentTableId',
            // url: '../api/table.json',	
			url:ipURL+"/caption/task/page",
            headers:{token:localStorage.token},
			parseData: function(res){ //res 即为原始返回的数据
			    return {
			      "code": res.code, //解析接口状态
			      "msg": res.message, //解析提示文本
			      "count": res.count, //解析数据长度
			      "data": res.data.data,//解析数据列表
				  
			    };	
				
			},
			
			response: {
			            statusCode: 0//规定成功的状态码，默认：0
			        },
			text:{none: '无数据'},
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true,hide:true},
                {field: 'name', title: '名称'},
                {field: 'teamName',  title: '所属团队'},
                {field: 'timeState', title: '状态' ,templet: '#status',},
                {field: 'finishState',  title: '提交状态'},
                {field: 'endTime',  title: '结束时间'},
                {title: '操作', minWidth: 300,  align: "center",
                    templet: function(d){

                        var operation = '';
                        if (d.filePath!=''){
                            operation += `<a class='layui-btn layui-btn-warm layui-btn-xs ' lay-event="showDescription" >查看作业</a>`;
                        }
                        operation += `<a class='layui-btn layui-btn-normal layui-btn-xs data-count-edit' lay-event="show" >查看记录</a>`;
                        operation+=" "
                        operation += `<a class='layui-btn layui-btn-normal layui-btn-xs data-count-edit' lay-event="edit" >编辑</a>`;
                        operation+=" "
                        operation += `<a class='layui-btn layui-btn-danger layui-btn-xs data-count-edit' lay-event="delete" >删除</a>`;
                        operation+=" "



                        return operation
                    }}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });






        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
			
			var taskName = $('input[name="taskName"]').val()
            //执行搜索重载
            table.reload('currentTableId', {
				
                page: {
                    curr: 1
                }
                , where: {
                    name:taskName,
                }
            });

            return false;
        });




        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                $('#name').attr("readonly",false)
                // 清空表单
                form.val("stuform", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id": "" // "name": "value"
                    ,"name": ""
                    ,"description": ""
                    ,"startTime":""
                    ,"endTime":""

                });
                form.on('select(sfgroupsFilter)', function(data){
                    if(data.value === "0") {
                        $('#rsxz').css('display', 'none');
                    } else {
                        $('#rsxz').css('display', 'block');
                    }
                });
                $('#rsxz').css('display', 'none');
                layer.open({
                    type: 1,
                    title: '发布任务',
                    area: ['550px', '400px'],
                    content: $('#save'),
                    btn: ['提交', '退出'],
                    end:function(){
                        $('#save').css('display','none')
                    },
                    yes:function(){
                    },

                    success:function(layero,index){

                        layui.use('form',function(){
                            var form=layui.form;
                            layero.addClass('layui-form');
                            var submitBtn=layero.find('.layui-layer-btn0');
                            submitBtn.attr('lay-filter','formVerify').attr('lay-submit','');
                            layero.keydown(function(e){
                                if(e.keyCode==13){
                                    submitBtn.click();
                                }
                            });

                            form.on('submit(formVerify)',function(data){
                                var formData = new FormData($( "#save" )[0]);
                                $.ajax({
                                    url: ipURL + '/caption/task',
                                    method: 'post',
                                    data: formData,
                                    cache: false,
                                    contentType: false,
                                    processData: false,
                                    beforeSend: function (request) {
                                        request.setRequestHeader("token", localStorage.token);
                                    },
                                    success: function (res) {
                                        if (res.code === 0){
                                            layer.msg(res.message);
                                            layer.close(index);
                                            layui.table.reload('currentTableId'); //重载父页表格，参数为表格ID
                                        }else{
                                            layer.msg(res.message);
                                        }
                                    },
                                    error: function (data) {
                                        layer.msg(res.message);
                                    }
                                });

                                return false;
                            });
                        });
                    }
                })
            }  if (obj.event === 'delete') {  // 监听删除操作

                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var ids = "";
                for(i=0;i<data.length;i++){
                    ids+=data[i].id;
                    ids+=",";
                }

                $.ajax({
                    url: ipURL + '/caption/task/'+ids,
                    method: 'delete',
                    beforeSend: function (request) {
                        request.setRequestHeader("token", localStorage.token);
                    },
                    success: function (res) {
                        layer.msg(res.message);
                        table.reload('currentTableId'); //刷新表格，参数为表格ID
                    },
                    error: function (data) {
                        layer.msg(data.message);
                    }
                });
				
            }
        });



        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                $('#name').attr("readonly",true)
                $("#teamList").attr("disabled","disabled")
                //给表单赋值
                form.val("stuform", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id": data.id // "name": "value"
                    ,"name": data.name
                    ,"description": data.description
                    ,"startTime":data.startTime
                    ,"endTime":data.endTime
                    ,"tId":data.tid
                    ,"isGroup":data.isGroup,
                    "groupUserLimit":data.groupUserLimit,
                    "fileNameTemplate":data.fileNameTemplate,

                });
                layer.open({
                    type: 1,
                    title: '修改',
                    area: ['550px', '400px'],

                    content: $('#save'),
                    btn: ['提交', '退出'],
                    end:function(){
                        $('#save').css('display','none')
                    },
                    yes:function(){
                    },

                    success:function(layero,index){

                        layui.use('form',function(){
                            var form=layui.form;
                            layero.addClass('layui-form');
                            var submitBtn=layero.find('.layui-layer-btn0');
                            submitBtn.attr('lay-filter','formVerify').attr('lay-submit','');
                            layero.keydown(function(e){
                                if(e.keyCode==13){
                                    submitBtn.click();
                                }
                            });

                            form.on('submit(formVerify)',function(data){
                                var formData = new FormData($( "#save" )[0]);

                                $.ajax({
                                    url: ipURL + '/caption/task',
                                    method: 'put',
                                    data: formData,
                                    cache: false,
                                    contentType: false,
                                    processData: false,
                                    beforeSend: function (request) {
                                        request.setRequestHeader("token", localStorage.token);
                                    },
                                    success: function (res) {
                                        layer.msg(res.message);
                                        if (res.state){
                                            layer.close(index);
                                            layui.table.reload('currentTableId'); //重载父页表格，参数为表格ID
                                        }
                                    },
                                    error: function (data) {
                                        layer.msg(res.message);
                                    }
                                });


                                return false;
                            });
                        });
                    }
                })
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('此项操作会把任务记录一并删除,请三思', function (index) {
                    obj.del();
                    $.ajax({
                        url: ipURL + '/caption/task/'+data.id,
                        method: 'delete',
                        beforeSend: function (request) {
                            request.setRequestHeader("token", localStorage.token);
                        },
                        success: function (res) {
                            layer.msg(res.message);
                            layui.table.reload('currentTableId'); //重载父页表格，参数为表格ID

                        },
                        error: function (data) {
                            layer.msg(data.message);
                        }
                    });
                    layer.close(index);
                });

            }else if (obj.event === 'show') {

                var index = layer.open({
                    title: data.name + "任务收集信息",
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'task-record.html',
                    success: function (layero, index) {
                        var iframe = window['layui-layer-iframe' + index];

                        iframe.child(data);
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }else if(obj.event === 'showDescription'){
                // 查看文件
                var filePath =  data.filePath; //要预览文件的访问地址
                if(filePath === ''){
                    layer.msg("文件路径为空")
                    return;
                }
                window.open('http://your remote IP:8012/onlinePreview?url='+filePath);

            }
        });

    });

    layui.use('laydate', function() {
        var laydate = layui.laydate;
        // 开始时间日期
        laydate.render({
            elem: '#test6'
            ,type: 'datetime'
            ,range: ['#startTime', '#endTime']
        });


    });

    function addTemplate() {
        layer.open({
            type: 1,
            title: '模板',
            area: ['550px', '500px'],

            content: $('#temp'),
            btn: ['校验模板','提交模板', '退出'],
            end:function(){
                $('#temp').css('display','none')
            },
            yes:function(){
                $.ajax({
                    url: ipURL + '/parseTemp',
                    method: 'post',
                    data: {
                        template:$('#inputFileNamePlate').val()
                    },
                    beforeSend: function (request) {
                        request.setRequestHeader("token", localStorage.token);
                    },
                    success: function (res) {
                        layer.msg(res.message);
                        if (res.state){
                            layer.close(index);
                        }
                    },
                    error: function (data) {
                        layer.msg(data.message);
                    }
                });
            },

            btn2:function(layero,index){
                $('#fileNamePlate').val($('#inputFileNamePlate').val())
            }
        })
    }

</script>

</body>


<form class="layui-form myData" action="save" method="post" enctype="multipart/form-data" lay-filter="stuform" id="save" style="display: none" >
    <input type="hidden" name="id"  />

    <div class="layui-form-item">
        <label class="layui-form-label">任务名称：</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required"  class="layui-input" id="name"/>
        </div>
    </div>


    <div class="layui-form-item" id="rsxz">
        <label class="layui-form-label">组队人数限制：</label>
        <div class="layui-input-block">
            <input type="text" name="groupUserLimit"  class="layui-input" value="0"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">任务所属团队：</label>
        <div class="layui-input-block">
            <select id="teamList" lay-verify="required" lay-filter="xmFilter" class="teamList" lay-search="" name="tId">
                <option value="">直接选择或搜索选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">文件名模板：</label>
        <div class="layui-input-block">
            <input type="text" name="fileNameTemplate" class="layui-input" id="fileNamePlate"/><button type="button" class="layui-btn" onclick="addTemplate()">添加模板</button>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">上传作业描述(word)：</label>
        <div class="layui-input-block">
            <input type="file" name="file"  id="file"/>
        </div>
    </div>

    <div class="layui-form">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">日期范围:</label>
                <div class="layui-inline" id="test6">
                    <div class="layui-input-inline">
                        <input type="text" autocomplete="off" id="startTime" name="startTime" class="layui-input" placeholder="开始日期">
                    </div>
                    <div class="layui-form-mid">-</div>
                    <div class="layui-input-inline">
                        <input type="text" autocomplete="off" id="endTime" name="endTime" class="layui-input" placeholder="结束日期">
                    </div>
                </div>
            </div>
        </div>
    </div>


</form>



<form class="layui-form myData" action="save" method="post" lay-filter="stuform" id="temp" style="display: none" >
    <div class="layui-form-item" >
        <label class="layui-form-label">模板样例:</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="{name}{studentNumber}" readonly/>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">模板解析后:</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="张三22B120" readonly/>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">模板样例:</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="{name}{studentNumber} Java作业" readonly/>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">模板解析后:</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="张三22B120 Java作业" readonly/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">可选择的模板变量</label>
        <div class="layui-input-block">
            <select name="city" lay-verify="required">
                <option value=""></option>
                <option value="0">昵称: {name}</option>
                <option value="1">学号: {studentNumber}</option>
                <option value="2">邮箱: {email}</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">输入相应模板(模板中不能出现+ / \)：</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" id="inputFileNamePlate" />
        </div>
    </div>
</form>

</html>